#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import time
import select
from Xlib import X, display, Xutil

class Painter:
    def __init__(self):
        self.d = display.Display()
        self.screen = self.d.screen()
        self.window = self.screen.root
        colormap = self.screen.default_colormap
        color = colormap.alloc_color(0, 0, 65535)
        self.gc = self.window.create_gc(
            line_width = 3,
            line_style = X.LineSolid,
            fill_style = X.FillOpaqueStippled,
            fill_rule  = X.WindingRule,
            cap_style  = X.CapButt,
            join_style = X.JoinMiter,
            foreground = color.pixel,
            background = self.screen.black_pixel,
            function = X.GXcopy, 
            graphics_exposures = False,
            subwindow_mode = X.IncludeInferiors,
        )
        self.pontos = []
    def adicionarPonto(self, x, y):
        x = int(x * self.screen.width_in_pixels)
        y = int(y * self.screen.height_in_pixels)
        self.pontos.append((x,y))
    def removerPontos(self):
        self.pontos = []
    def desenharTodos(self):
        tamanho = len(self.pontos)
        if tamanho < 2:
            return
        for i in range(tamanho - 1):
            p1 = self.pontos[i]
            p2 = self.pontos[i+1]
            self.window.line(self.gc, p1[0], p1[1], p2[0], p2[1])
        self.d.flush()
    def desenharUltimos(self):
        tamanho = len(self.pontos)
        if tamanho < 2:
            return
        p1 = self.pontos[-2]
        p2 = self.pontos[-1]
        self.window.line(self.gc, p1[0], p1[1], p2[0], p2[1])
        self.d.flush()
    def apagarAnotacoes(self):
        pass

if __name__ == '__main__':
    p = Painter()
    for i in range(400, 800, 2):
        p.adicionarPonto(i+20, i)
        p.desenharUltimos()
    p.removerPontos()
    p.adicionarPonto(0,0)
    p.adicionarPonto(100,100)
    p.desenharTodos()
    time.sleep(1)
    p.apagarAnotacoes()
